package com.glsc.ngateway.common.api.flowable.dto;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class Result<T> {
    public static Integer CODE_SUCCESS = 10000;
    public static Integer CODE_ERROR = 10001;
    public static Integer CODE_ERROR_10002 = 10002;
    public static Integer CODE_ERROR_10003 = 10003;
    public static Integer CODE_NOT_FOUND = 90001;
    public static Integer CODE_ERROR_PRODUCT_CENTER = 88001;

    /**
     * 账户类型办结字段未填写
     */
    public static Integer CODE_WORKFLOW_RELYCODE_UNDO_ACCOUNT = 11000;
    public static Integer CODE_WORKFLOW_RELYCODE_UNDO_PROD_INFO = 11001;

    public static String CODE = "code";
    public static String MESSAGE = "message";
    public static String DATA = "data";

    public static String MESSAGE_DEFAULT = "OK";

    //返回数据
    private Integer code;
    private String message;
    private T data;

    private static final Logger logger = LoggerFactory.getLogger(Result.class);

    public Result() {
        this(CODE_SUCCESS, MESSAGE_DEFAULT, null);
    }

    public Result(T data) {
        this(CODE_SUCCESS, MESSAGE_DEFAULT, data);
    }

    public Result(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static Map<String, Object> fail(Integer code, String message) {
        Map<String, Object> result = new HashMap<>();
        //Map<String, Object> data = new HashMap<>();
        //result.put("data", data);
        result.put(CODE, code);
        result.put(MESSAGE, message);
        return result;
    }

//    public static Map<String, Object> fail(Exception e) {
//        return fail(e, null);
//    }
//
//    public static Map<String, Object> fail(Exception e, String message) {
//        Map<String, Object> result = new HashMap<>();
//        if (null != e) {
//            if (e instanceof AmException) {
//                AmException amExcept = (AmException) e;
//
//                if (amExcept.getStatus() < 10000) {
//                    result.put(CODE, Result.CODE_ERROR_10002);
//                } else {
//                    result.put(CODE, amExcept.getStatus());
//                }
//                if (StrUtil.isBlank(message)) {
//                    result.put(MESSAGE, amExcept.getMessage());
//                } else {
//                    result.put(MESSAGE, message + "：" + amExcept.getMessage());
//                }
//            } else {
//                result.put(CODE, Result.CODE_ERROR_10002); //异常的默认错误信息
//                if (StrUtil.isBlank(message)) {
//                    result.put(MESSAGE, e.getMessage());
//                } else {
//                    result.put(MESSAGE, message + "：" + e.getMessage());
//                }
//            }
//        }
//        return result;
////        return new Result((Integer) result.get(CODE), (String)result.get(MESSAGE), null);
//    }

    public static Map<String, Object> success(String message) {
        Map<String, Object> result = new HashMap<>();
        result.put(CODE, CODE_SUCCESS);
        result.put(MESSAGE, message);
        return result;
    }

    public static <T> Result success(String message, T data) {
        return new Result(CODE_SUCCESS, message, data);
    }

    public static Result successMessage(String message) {
        return new Result(CODE_SUCCESS, message, null);
    }

    public static <T> Result successData(T data) {
        return new Result(CODE_SUCCESS, MESSAGE_DEFAULT, data);
    }

    public static Result failData(String failMessage) {
        return new Result(Result.CODE_ERROR_10002, failMessage, null);
    }

    public static Result notFound(String notFoundMessage) {
        return new Result(Result.CODE_NOT_FOUND, notFoundMessage, null);
    }

    //默认错误码
    public static Map<String, Object> fail(String message) {
        return fail(CODE_ERROR, message);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data='" + (null == data ? "NULL" : data.toString()) +
                '}';

    }
}
